package vivek.project.pocketmoney.expenses.model
{
	import mx.charts.*;
	import mx.collections.ArrayCollection;
	import mx.collections.IViewCursor;
	import mx.collections.ListCollectionView;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import vivek.project.pocketmoney.admin.model.AdminConstants;

	public class ExpenseSummaryModel
	{
		[Bindable]
		public static var yearData:ArrayCollection=new ArrayCollection();
		private static var instance:ExpenseSummaryModel;

		/*[Bindable (event="expenseSummaryChanged")]
		   private function refreshSummaryData(e:Event):void
		   {
		   //refreshDailyData();
		   //refreshDetailData();
		   //refreshSummaryData();
		   trace("a");
		 }*/
		public static function getInstance():ExpenseSummaryModel
		{
			if (!instance)
			{
				instance=new ExpenseSummaryModel();
			}
			return instance;
		}

		public function ExpenseSummaryModel():void
		{
			init();
			//ExpenseDataCache.expenseData.addEventListener(CollectionEvent.COLLECTION_CHANGE,refreshSummaryData);
		}
		[Bindable]
		private var dailyData:ArrayCollection=new ArrayCollection();
		[Bindable]
		private var detailsData:ArrayCollection=new ArrayCollection();
		[Bindable]
		private var selectedMonth:int=9;
		[Bindable]
		private var selectedYear:int=2008;
		[Bindable]
		private var yearDataCollection:ListCollectionView=new ListCollectionView();

		private function init():void
		{
			//yearDataCollection.list=ExpenseDataCache.expenseSummaryData;
			var cursor:IViewCursor=yearData.createCursor();
			var sort:Sort=new Sort();
			sort.fields=[new SortField("em_year", false), new SortField("em_month", false), new SortField("em_category", false)];
			yearData.sort=sort;
			yearData.refresh();
			var obj:Object=new Object();
			for each (obj in yearDataCollection)
			{
				var obj1:Object=new Object();
				obj1.em_year=obj.em_year;
				if (cursor.findAny(obj1))
				{
					var obj2:Object=new Object();
					obj2=cursor.current;
					if (obj.em_expensetype == AdminConstants.WITHDRAWAL)
					{
						obj2.em_withdrawal=obj2.em_withdrawal + (obj.em_amount * -1);
						obj2.em_amount=obj2.em_amount - obj.em_amount;
					}
					else
					{
						obj2.em_deposit=obj2.em_deposit + obj.em_amount;
						obj2.em_amount=obj2.em_amount + obj.em_amount;
					}
				}
				else
				{
					var obj3:Object=new Object();
					obj3.em_year=obj.em_year;
					obj3.em_amount=0.00;
					obj3.em_withdrawal=0.00;
					obj3.em_deposit=0.00;
					if (obj.em_expensetype == AdminConstants.WITHDRAWAL)
					{
						obj3.em_withdrawal=obj.em_amount * -1;
						obj3.em_amount=obj3.em_amount - obj.em_amount;
					}
					else
					{
						obj3.em_deposit=obj.em_amount;
						obj3.em_amount=obj3.em_amount + obj.em_amount;
					}
					yearData.addItem(obj3);
				}
			}
		}

		private function refreshDailyData(event:ChartItemEvent):void
		{
			dailyData.source=new Array(); //.removeAll();
			var obj:Object=new Object();
			var sort:Sort=new Sort();
			sort.fields=[new SortField("em_year", false), new SortField("em_month", false)];
			yearDataCollection.sort=sort;
			yearDataCollection.refresh();
			var dailySort:Sort=new Sort();
			dailySort.fields=[new SortField("em_day", false)];
			dailyData.sort=dailySort;
			dailyData.refresh();
			var cursor:IViewCursor=yearDataCollection.createCursor();
			var dailyCursor:IViewCursor=dailyData.createCursor();
			var month:int=event.hitData.item.em_month;
			selectedMonth=month;
			var obj1:Object=new Object();
			obj1.em_month=month;
			while (!cursor.afterLast)
			{
				var currentObject:Object=new Object();
				currentObject=cursor.current;
				var dailyObj:Object=new Object();
				dailyObj.em_day=currentObject.em_day;
				if (currentObject.em_year == selectedYear && currentObject.em_month == month)
				{
					if (dailyCursor.findAny(dailyObj))
					{
						var obj2:Object=new Object();
						obj2=dailyCursor.current;
						obj2.em_day=currentObject.em_day;
						if (currentObject.em_expensetype == AdminConstants.WITHDRAWAL)
						{
							obj2.em_withdrawal=obj2.em_withdrawal - currentObject.em_amount;
							obj2.em_amount=obj2.em_amount - currentObject.em_amount;
						}
						else
						{
							obj2.em_deposit=obj2.em_deposit + currentObject.em_amount;
							obj2.em_amount=obj2.em_amount + currentObject.em_amount;
						}
					}
					else
					{
						var obj3:Object=new Object();
						obj3.em_day=currentObject.em_day;
						obj3.em_amount=0.00;
						obj3.em_withdrawal=0.00;
						obj3.em_deposit=0.00;
						if (currentObject.em_expensetype == AdminConstants.WITHDRAWAL)
						{
							obj3.em_withdrawal=obj3.em_withdrawal - currentObject.em_amount;
							obj3.em_amount=obj3.em_amount - currentObject.em_amount;
						}
						else
						{
							obj3.em_deposit=obj3.em_deposit + currentObject.em_amount;
							obj3.em_amount=obj3.em_amount + currentObject.em_amount;
						}
						dailyData.addItem(obj3);
					}
				}
				cursor.moveNext();
			}
			dailyData.refresh();
		}

		private function refreshDetailData(event:Event):void
		{
			detailsData.source=new Array(); //removeAll();
			dailyData.source=new Array(); //.removeAll();
			var obj:Object=new Object();
			var sort:Sort=new Sort();
			sort.fields=[new SortField("em_year", false), new SortField("em_month", false), new SortField("em_category", false)];
			var monthSort:Sort=new Sort();
			monthSort.fields=[new SortField("em_month", false)];
			detailsData.sort=monthSort;
			detailsData.refresh();
			yearDataCollection.sort=sort;
			yearDataCollection.refresh();
			var cursor:IViewCursor=yearDataCollection.createCursor();
			var monthCursor:IViewCursor=detailsData.createCursor();
			var year:int=event.hitData.item.em_year;
			selectedYear=year;
			var obj1:Object=new Object();
			obj1.em_year=year;
			while (!cursor.afterLast)
			{
				var currentObject:Object=new Object();
				currentObject=cursor.current;
				var monthObj:Object=new Object();
				monthObj.em_month=currentObject.em_month;
				if (currentObject.em_year == year)
				{
					if (monthCursor.findAny(monthObj))
					{
						var obj2:Object=new Object();
						obj2=monthCursor.current;
						obj2.em_month=currentObject.em_month;
						if (currentObject.em_expensetype == AdminConstants.WITHDRAWAL)
						{
							obj2.em_withdrawal=obj2.em_withdrawal - currentObject.em_amount;
							obj2.em_amount=obj2.em_amount - currentObject.em_amount;
						}
						else
						{
							obj2.em_deposit=obj2.em_deposit + currentObject.em_amount;
							obj2.em_amount=obj2.em_amount + currentObject.em_amount;
						}
					}
					else
					{
						var obj3:Object=new Object();
						obj3.em_month=currentObject.em_month;
						obj3.em_amount=0.00;
						obj3.em_withdrawal=0.00;
						obj3.em_deposit=0.00;
						if (currentObject.em_expensetype == AdminConstants.WITHDRAWAL)
						{
							obj3.em_withdrawal=obj3.em_withdrawal - currentObject.em_amount;
							obj3.em_amount=obj3.em_amount - currentObject.em_amount;
						}
						else
						{
							obj3.em_deposit=obj3.em_deposit + currentObject.em_amount;
							obj3.em_amount=obj3.em_amount + currentObject.em_amount;
						}
						detailsData.addItem(obj3);
					}
				}
				cursor.moveNext();
			}
			detailsData.refresh();
		}
	}
}